package kr.util.extractor;

import java.io.IOException;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;

public class PdfExtractor extends AbstractExtractor{
	
	public PdfExtractor(String fileName){
		super(fileName);
	}
	
	@Override
	public void setText(String fileName) {
		StringBuffer tmpText = new StringBuffer(); 
		
		PdfReader reader = null;
		try {
			reader = new PdfReader(fileName);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        PdfReaderContentParser parser = new PdfReaderContentParser(reader);
        TextExtractionStrategy strategy = null;
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            try {
				strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            
            tmpText.append(strategy.getResultantText() + "\n");
        }
        
        this.text = tmpText;
	}
	
	public static void main(String[] args){
		PdfExtractor pe = new PdfExtractor("http://db.apache.org/derby/docs/10.8/getstart/getstartderby.pdf");
		System.out.println(pe.getText().toString());
	}
}
